package com.yunfeisoft.business.dao.impl.postgres;

import com.applet.base.ServiceDaoImpl;
import com.applet.sql.builder.SelectBuilder;
import com.applet.sql.builder.WhereBuilder;
import com.applet.utils.Page;
import com.yunfeisoft.business.dao.inter.SmsHomeNewProductDao;
import com.yunfeisoft.business.model.PmsProduct;
import com.yunfeisoft.business.model.SmsHomeNewProduct;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;

/**
 * ClassName: SmsHomeNewProductDaoImpl
 * Description: 新鲜好物表Dao实现
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Repository
public class SmsHomeNewProductDaoImpl extends ServiceDaoImpl<SmsHomeNewProduct, String> implements SmsHomeNewProductDao {

    @Override
    public Page<SmsHomeNewProduct> queryPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("createTime");
        if (params != null) {
            if (!ObjectUtils.isEmpty(params.get("name"))) {
                wb.andFullLike("productName", params.get("name"));
            }
            initPageParam(wb, params);
        }
        return queryPage(wb);
    }

    @Override
    public List<SmsHomeNewProduct> queryByProductId(String productId) {
        WhereBuilder wb = new WhereBuilder();
        wb.andEquals("productId", productId);
        return query(wb);
    }

    /**
     * 分页查询新鲜好物列表
     * @param params
     * @return
     */
    @Override
    public Page<SmsHomeNewProduct> queryHomeNewProductPage(Map<String, Object> params) {
        WhereBuilder wb = new WhereBuilder();
        if (params != null) {
            initPageParam(wb, params);
        }
        wb.setOrderByWithDesc("n.sort");
        wb.addOrderByWithDesc("n.createTime");
        SelectBuilder builder = getSelectBuilder("n");
        builder.column("p.price as price")
                .join(PmsProduct.class)
                .alias("p")
                .on("n.productId = p.id").build();
        return queryPage(builder.getSql(), wb);
    }

    /**
     * 查询首页新鲜好物列表
     * @param limit
     * @return
     */
    @Override
    public List<SmsHomeNewProduct> queryHomeNewProductIndex(int limit) {
        WhereBuilder wb = new WhereBuilder();
        wb.setOrderByWithDesc("n.sort");
        wb.addOrderByWithDesc("n.createTime");
        SelectBuilder builder = getSelectBuilder("n");
        builder.column("p.price as price")
                .join(PmsProduct.class)
                .alias("p")
                .on("n.productId = p.id").build();
        if (limit > 0) {
            wb.setPageNum(1);
            wb.setPageSize(limit);
            return queryLimit(builder.getSql(), wb);
        }
        return query(builder.getSql(), wb);
    }
}